/*
 * @Descripttion: 
 * @version: 
 * @Author: sueRimn
 * @Date: 2022-10-28 16:04:14
 * @LastEditors: sueRimn
 * @LastEditTime: 2023-02-27 14:09:37
 */

let util = require("../util");
let operate = require("../db/opeate")
let db = require("../db/index");
let { query } = require("../db/pool")
exports.getFormById = (req, res) => {
  let params = req.body;
  let strSql = 'select * from page where id="' + params.formId + '"';
  getPageInfo(strSql, res)
}
async function getPageInfo(sql, res) {
  let rtn = await query(sql);
  if (rtn.status) {
    let dt = rtn.data[0];
    let result = {
      data: {
        formCode: dt.code,
        requestList: [],
        component: "",
      },
      message: "ok",
      status: 1
    };
    let cpArr = dt.page_component ? dt.page_component.split(",") : [];
    let page_component_arr = [];
    for (let k = 0; k < cpArr.length; k++) {
      str = 'select * from component where id = "' + cpArr[k] + '";';
      let trtn = await query(str);
      page_component_arr.push(trtn.data[0]);
    }
    let dvArr = dt.page_data_view ? dt.page_data_view.split(",") : [];
    let page_data_view_arr = [];
    for (let k = 0; k < dvArr.length; k++) {
      str = 'select * from data_view where id = "' + dvArr[k] + '";';
      let trtn = await query(str);
      page_data_view_arr.push(trtn.data[0]);
    }
    let comTypeArr = "[{";
    page_component_arr.forEach(item => {
      switch (item.type) {
        case "formPage":
          comTypeArr += "formPage:'" + item.content.toString();
          break;
        case "infoPage":
          comTypeArr += "infoPage:'" + item.content.toString();
          break;
        case "fixedLayout":
          comTypeArr += "fixedLayout:'" + item.content.toString();
          break;
        case "operaterArea":
          comTypeArr += "operaterArea:'" + item.content.toString();
          break;
        default:
          console.log("组件类型错误");
      }
    })
    comTypeArr += "',";
    comTypeArr += 'dataView:{';
    page_data_view_arr.forEach(item => {
      comTypeArr += item.component_code + 'Data:originObj,';
      comTypeArr += item.component_code + 'Element:' + item.content + ",";
    })
    comTypeArr += '},}]';
    for (const key in comTypeArr) {
      if (comTypeArr[key] == "") {
        delete comTypeArr[key];
      }
    }
    result.data.component = comTypeArr;
    //处理请求
    let rqArr = dt.page_request ? dt.page_request.split(",") : [];
    let page_request_arr = [];
    for (let k = 0; k < rqArr.length; k++) {
      str = 'select * from request_config where id = "' + rqArr[k] + '";';
      let trtn = await query(str);
      page_request_arr.push(trtn.data[0]);
    }
    result.data.requestList = page_request_arr;
    res.send(result);
  } else {
    res.send(rtn);
  }
}

exports.getRequestTestList = (req, res) => {
  let params = req.body;
  let tmpRtn = [{
    id: new Date().getTime(),
    name: "dlj",
    age: 22
  }, {
    id: new Date().getTime(),
    name: "dlj1",
    age: 21
  }];
  let result = {
    data: tmpRtn,
    message: "ok",
    status: 1
  };
  res.send(result)
}